<?php
error_reporting(0);

require_once "common.inc.php";

page_header("Geoteamové statistiky logů");

include_once "homepage.inc.php";




if($wp = $_GET['wp']){
	
	$r = dibi::fetch("SELECT * FROM caches WHERE waypoint = %s",$wp,"");

	echo "<h2>Keš $wp - ";
	echo cacheIco($r) . " <a href='http://coord.info/$wp' title='online'>$r[name]</a>";
	echo "</h2>";
	echo "<p>$r[cache_type] by $r[owner] ($r[size] $r[stars]) \n";
	
	foreach(dibi::query("
			SELECT *
			FROM logs LEFT JOIN caches USING(cache_id)
			WHERE waypoint = %s",$wp,"
			ORDER BY date DESC, id DESC") as $r){
		
		echo "<div class='loghead' title='dtb #$r[id]'>";
			echo "<span class='left'>" . poradi($r) ." ". logIco($r) . " $r[date]</span>";
			echo " <a href='?cacher=$r[cacher_id]#log$poradi'>".getCacher($r['cacher_id'])."</a>";
		echo "</div>";
		echo "<div class='logtext'>$r[text]</div>\n";

	}

	page_footer();
	exit;
}

if($cacher_id = $_GET['cacher']){
	echo "<p style='float:right'><a href='http://www.geocaching.com/profile/?guid=".getCacherGuid($cacher_id)."'><img src='http://img.geocaching.com/stats/img.aspx?txt=Profil+na+Geocaching.com&uid=".getCacherGuid($cacher_id)."' border='0'></a>";
	echo "<h2>Kešer ".getCacher($cacher_id)."</h2>";
	
	$r = dibi::fetch("
		SELECT SUM( LENGTH(text) - LENGTH(REPLACE(text, ' ', ''))+1) slova, count(1) pocet
		FROM logs
			WHERE cacher_id = %i",$cacher_id,"
			AND type = 'smile'");
	
	
	echo "<p>Počet found logů: $r[pocet], průměrný počet slov na log: ". ($r['pocet'] ? round($r['slova'] / $r['pocet']) : 'NaN');
	
	echo "<p>Typy keší: ";
	$r = dibi::fetchAll("
		SELECT distinct cache_type AS cache_type, sum(1) as pocet
		FROM logs
		LEFT JOIN caches USING(cache_id)
		WHERE cacher_id = %i",$cacher_id,"
			AND type = 'smile'
		GROUP BY cache_type
		ORDER BY pocet DESC");
	foreach($r as $row){
		echo cacheIco($row)." $row[pocet]× ";
	}
	
	$predchoziDen = "";
	$parita = 'sudy';

	echo "<div class='$parita'>";
	/*$res = dibi::query("
		SELECT *, GROUP_CONCAT(DISTINCT cacher_id) spolukaceri
		FROM logs LEFT JOIN caches USING(cache_id)
		GROUP BY cache_id
		HAVING cacher_id = %i",$cacher_id,"
		ORDER BY date DESC, id ASC");*/
	$res = dibi::query("
		SELECT *
		FROM logs LEFT JOIN caches USING(cache_id)
		WHERE cacher_id = %i",$cacher_id,"
		ORDER BY date DESC, id ASC");		
	foreach($res as $r){
		$spoluSup = spolukaceri($r, $cacher_id);
		$spoluTitle = spolukaceri($r);
		
		if($predchoziDen != $r['date']) {
			$parita = ($parita=='sudy') ? 'lichy' : 'sudy';
			$predchoziDen = $r['date'];
			echo "</div>";
			echo "<div class='$parita'>";
		}
		
		echo "<div class='loghead'$spoluTitle>";
			echo "<span class='left'>" . poradi($r) ." ". logIco($r) . " $r[date]</span>"
			   . cacheIco($r) . " <a href='?wp=$r[waypoint]'>$r[name]</a>$spoluSup"
			   . " ($r[size] $r[stars]) <small>by $r[owner]</small>";
		echo "</div>";
		echo "<div class='logtext'>$r[text]</div>\n";
	}
	echo "</div>";

	page_footer();
	exit;
}




echo "<table><tr><td>";
foreach(getCachers() as $v)
	echo "<th>$v\n";

$r = dibi::query("SELECT cacher_id,YEAR(date) year, MONTH(date) month,count(*) founds FROM logs WHERE type='smile' AND YEAR(date)!=0 GROUP BY cacher_id,YEAR(date),MONTH(date) ORDER BY date DESC");
foreach($r->fetchAssoc('year,month,cacher_id') as $year=>$r1){
	foreach($r1 as $month=>$row){
		echo "<tr><td>$year/$month";
		foreach(getCachers() as $cacher_id=>$name)
			echo "<td>".@$row[$cacher_id]['founds'];
	}
}
echo "</table>";




echo "<p>By cache: <br>";

foreach(dibi::query("
		SELECT *, count(logs.id) i
		FROM caches LEFT JOIN logs USING(cache_id)
		WHERE type='smile'
		GROUP BY caches.cache_id
		ORDER BY i DESC
		") as $r){
		if($r['i'] > 2)
	echo "$r[i] " . cacheIco($r) . "<a href='?wp=$r[waypoint]'>$r[name]</a> <br>";
}


page_footer();
exit;


/* ---------- šablonové funkce ----------- */


function spolukaceri($r, $sup=false){
	return "";
	static $mem, $lastid;
	if($lastid != $r['id']){
		$mem = dibi::fetchSingle("SELECT GROUP_CONCAT(DISTINCT cacher_id)
			FROM logs 
			WHERE cache_id = %i",$r['cache_id'],"
			AND cacher_id != %i",$r['cacher_id'],"
			GROUP BY cache_id");
		$lastid = $r['id'];
	}
	
	$arr = explode(',', $mem);
	if($sup){
		if (count($arr)>1) return "<sup>+".(count($arr))."</sup> ";
		if (count($arr)==1) return "<sup>+".getCacher($arr[0])."</sup> ";
		return "";
	}
	
	$o = '';
	foreach($arr as $cid){
		if($o) $o .= ', ';
		$o .= getCacher($cid);		
	}
	return $o ? " title='Logováno mají: $o'" : '';
}

function cacheIco($r){
	return "<img src='img/cachetype/".strtolower(substr($r["cache_type"], 0,4)).".gif'>";
}
function logIco($r){
	if($r['type'] == 'smile' AND strpos(strtolower($r['cache_type']), 'event') !== false)
		 $r['type'] = 'attended';
	if($r['type'] == 'smile' AND strpos(strtolower($r['cache_type']), 'webcam') !== false)
		 $r['type'] = 'camera';
		 
	return "<img src='http://www.geocaching.com/images/icons/icon_$r[type].gif' alt='$r[type]' title='$r[type]'>";
}
function poradi($r){
	$res = dibi::fetch("
		SELECT sum(date < %s",$r['date'],")+1 minule,
		       sum(date = %s",$r['date']," AND id > %i",$r['id'],") dnesni,
		       sum(date = %s",$r['date'],") dnes
		FROM logs
		WHERE cacher_id=%i",$r['cacher_id']," 
			AND `type` = 'smile'
		");
	$logurl = "http://www.geocaching.com/seek/log.aspx?LUID=";
	$poradi = $res["minule"] + $res["dnesni"];
	
	if($r["type"] == "smile"){
		$title = "#$res[minule]" . ($res['dnes']>1 ? " + $res[dnesni] (z $res[dnes] tento den)":""); 
		return "<a href='$logurl$r[luid]' title='$title' id='log$poradi'>#$poradi</a>";
	}

	return "<a href='$logurl$r[luid]' style='text-decoration:none'>---</a>";
}
